package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseEquipmentInfoMapper;
import com.redoor.app.stake.basicdata.model.BaseEquipmentInfo;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseEquipmentInfoActionBean;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseEquipmentInfoCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseEquipmentInfoServiceBean;
import com.redoor.app.stake.basicdata.service.BaseEquipmentInfoService;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.basicdata.service.BaseVendorInfoService;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import com.yotouch.app.stake.model.Equipment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by wangjunlei on 2018-03-12 17:30:28.
 */
@Service
public class BaseEquipmentInfoServiceImpl extends AbstractService<BaseEquipmentInfo, String> implements BaseEquipmentInfoService {

    @Autowired
    private BaseEquipmentInfoMapper baseEquipmentInfoMapper;

    //厂商service
    @Autowired
    private BaseVendorInfoService baseVendorInfoService;
    //用户业务层
    @Autowired
    private BaseUserService baseUserService;
    @Autowired
    private BaseStakeService baseStakeService;

    private final Logger logger = LoggerFactory.getLogger(BaseEquipmentInfoServiceImpl.class);

    @Override
    public BaseEquipmentInfo getEquipmentByParams(BaseEquipmentInfoCriteria criteria) {
        if (criteria==null){
            return null;
        }
        List<BaseEquipmentInfo> baseEquipmentInfos = baseEquipmentInfoMapper.queryEquipments(criteria);
        BaseEquipmentInfo equipmentInfo = (baseEquipmentInfos!=null && baseEquipmentInfos.size()>0)?baseEquipmentInfos.get(0):null;
        return equipmentInfo;
    }

    @Override
    public List<BaseEquipmentInfoServiceBean> qryEquipmentInfo(BaseEquipmentInfoCriteria criteria) {
        return this.baseEquipmentInfoMapper.qryEquipmentInfo(criteria);
    }

    @Override
    public int insertEquipmentInfo(BaseEquipmentInfoActionBean baseEquipmentInfoActionBean) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseEquipmentInfo baseEquipmentInfo = new BaseEquipmentInfo();
        baseEquipmentInfo.setUuid(UUID.randomUUID().toString());
        baseEquipmentInfo.setCreatedat(new Date());
        baseEquipmentInfo.setCreatoruuid(loginUser.getUuid());
        baseEquipmentInfo.setUpdatedat(baseEquipmentInfo.getCreatedat());
        baseEquipmentInfo.setUpdateruuid(baseEquipmentInfo.getCreatoruuid());
        baseEquipmentInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        //前台传入数据
        baseEquipmentInfo.setType(baseEquipmentInfoActionBean.getType());
        baseEquipmentInfo.setEquipmentno(baseEquipmentInfoActionBean.getEquipmentNo());
        baseEquipmentInfo.setEquipmentname(baseEquipmentInfoActionBean.getEquipmentName());
        baseEquipmentInfo.setChargestandard(baseEquipmentInfoActionBean.getChargeStandard());
        baseEquipmentInfo.setVendoruuid(baseEquipmentInfoActionBean.getVendorUuid());
        baseEquipmentInfo.setPower(Double.valueOf(baseEquipmentInfoActionBean.getPower()));
        baseEquipmentInfo.setMinoutvolt(Double.valueOf(baseEquipmentInfoActionBean.getMinOutVolt()));
        baseEquipmentInfo.setMaxoutvolt(Double.valueOf(baseEquipmentInfoActionBean.getMaxOutVolt()));
        baseEquipmentInfo.setMinoutcurrent(Double.valueOf(baseEquipmentInfoActionBean.getMinOutCurrent()));
        baseEquipmentInfo.setMaxoutcurrent(Double.valueOf(baseEquipmentInfoActionBean.getMaxOutCurrent()));
        return this.baseEquipmentInfoMapper.insert(baseEquipmentInfo);
    }

    @Override
    public int updateEquipmentInfoByUuid(BaseEquipmentInfoActionBean baseEquipmentInfoActionBean) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseEquipmentInfo baseEquipmentInfo = new BaseEquipmentInfo();
        baseEquipmentInfo.setUpdatedat(new Date());
        baseEquipmentInfo.setUpdateruuid(loginUser.getUuid());
        //前台传入数据
        baseEquipmentInfo.setUuid(baseEquipmentInfoActionBean.getUuid());
        baseEquipmentInfo.setType(baseEquipmentInfoActionBean.getType());
        baseEquipmentInfo.setEquipmentno(baseEquipmentInfoActionBean.getEquipmentNo());
        baseEquipmentInfo.setEquipmentname(baseEquipmentInfoActionBean.getEquipmentName());
        baseEquipmentInfo.setChargestandard(baseEquipmentInfoActionBean.getChargeStandard());
        baseEquipmentInfo.setVendoruuid(baseEquipmentInfoActionBean.getVendorUuid());
        baseEquipmentInfo.setPower(Double.valueOf(baseEquipmentInfoActionBean.getPower()));
        baseEquipmentInfo.setMinoutvolt(Double.valueOf(baseEquipmentInfoActionBean.getMinOutVolt()));
        baseEquipmentInfo.setMaxoutvolt(Double.valueOf(baseEquipmentInfoActionBean.getMaxOutVolt()));
        baseEquipmentInfo.setMinoutcurrent(Double.valueOf(baseEquipmentInfoActionBean.getMinOutCurrent()));
        baseEquipmentInfo.setMaxoutcurrent(Double.valueOf(baseEquipmentInfoActionBean.getMaxOutCurrent()));
        return this.baseEquipmentInfoMapper.updateByPrimaryKeySelective(baseEquipmentInfo);
    }

    @Override
    public int updateStatus(String id) {
        //根据设备型号查询绑定桩数量
        int count = this.baseStakeService.qryStakeCountByEquipmentUuid(id);
        //当型号绑定桩后返回状态码-1
        if(count >0){
            return -1 * count;
        }
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseEquipmentInfo baseEquipmentInfo = new BaseEquipmentInfo();
        baseEquipmentInfo.setUpdatedat(new Date());
        baseEquipmentInfo.setUpdateruuid(loginUser.getUuid());
        //修改状态为1001
        baseEquipmentInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
        //前台传入数据
        baseEquipmentInfo.setUuid(id);
        int status = this.baseEquipmentInfoMapper.updateByPrimaryKeySelective(baseEquipmentInfo);
        return status;
    }

    @Override
    public Long qryEquipmentInfoTotal(BaseEquipmentInfoCriteria criteria) {
        return this.baseEquipmentInfoMapper.qryEquipmentInfoTotal(criteria);
    }

    @Override
    public Integer qryEquipmentInfoCountByName(String name, String vendorUuid) {
        BaseEquipmentInfo baseEquipmentInfo = new BaseEquipmentInfo();
        baseEquipmentInfo.setEquipmentname(name);
        baseEquipmentInfo.setVendoruuid(vendorUuid);
        baseEquipmentInfo.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        //判断数据库数据是否有名称重复
        int  count = this.baseEquipmentInfoMapper.selectCount(baseEquipmentInfo);
        //当有重复异常数据时报出错误,并返回第一条数据保证服务正常
        if(count > 1){
            logger.error("数据库后台名称有重复需要处理,名称为" + name);
        }
        return count;
    }

    @Override
    public BaseEquipmentInfoServiceBean qryEquipmentInfoByUuid(String id) {
        return this.baseEquipmentInfoMapper.qryEquipmentInfoByUuid(id);
    }

    @Override
    public List<Equipment> qryEquipmentInfoToHSF(Map<String, Object> map) {
        //查询设备信息
        List<Equipment> list = this.baseEquipmentInfoMapper.qryEquipmentInfoToHSF(map);
        //循环将设备对应的厂商信息放入对象
        for (Equipment equipment:list ) {
            equipment.setVendor(this.baseVendorInfoService.qryVendorInfoByVendorUuidToHSF(equipment.getVendorUuid()));
        }

        return list;
    }

    @Override
    public List<BaseEquipmentInfo> qryEquipmentInfoByVendorUuid(String vendorUuid) {
        BaseEquipmentInfo param = new BaseEquipmentInfo();
        param.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        param.setVendoruuid(vendorUuid);
        List<BaseEquipmentInfo> list = this.baseEquipmentInfoMapper.select(param);
        return list;
    }
}
